ODPS SQL 大全 | 您所在的位置:网站首页 › odps datediff函数 › ODPS SQL 大全 |
目录索引一、字符串函数二、窗口函数三、聚合函数四、日期函数五、数学函数
一、字符串函数
1.char_matchcount
bigint char_matchcount(string str1, string str2) 用途:用于计算字符串str1中的字符有多少个出现在字符串str2中; 2.charstring char(bigint ascii) 用途:将一个给定的ASCII码转换为字符 3.concat string concat(string a , string b …) 用途:将多个字符串凭凑成为一个字符串 4.get_json_objectstring get_json_object( string json, string path) 用途:在一个标准的json字符串中,按照path抽取指定的字符串 5.instrbigint instr(string str1, string str2, bigint strat_position, bigint nth_appearance) 用途:计算一个字符串str1在字符串str2中的位置 for example: instr(‘tech on the net’, ‘e’,1 ,2) = 11 6.is_encodingboolean is_encoding(string str, string from_encoding, string to_encoding) 用途:用于判断一个字符串是否可以从一个字符编码变成另外一种字符编码 for example: is_encoding(‘测试’, ‘utf-8’, ‘GBK’) = true; 7.keyvaluekeyvalue(string srcstr, string split1, string split2, string key); keyvalue(string srcstr, string key) //split1=’;’ , split2 = ‘:’ 用途:用两个分隔符分割后的键值对,给一个键返回一个数值;默认的两个分隔符为封号和冒号 for example: keyvalue(‘0:1\;1:2’, 1) = 2 8.lengthbigint length(string str) 用途:返回一个字符串的长度 9.lengthbbigint lengthb(string str) 用途:返回一个字符串有多少个字节,比如说,一个中文文字就是两个字节 10.md5string md5(string value) 用途:计算输入字符串value的md5值 11.regexp_extract(不懂)string regexp_extract(string source, string pattern, bigint occurrence) 用途: 12.regexp_instr(不懂) 13.regexp_replace 14.regexp_substr 15.regexp_count 16.split_part string split_part(string str, string separator, bigint start[, bigint end]) 用途:按照分隔符separator分割字符串,返回从start开始到end结束的字符串 17.substrstring substr(string str, bigint start_position[, bigint length]) 用途:返回字符串str从start_position开始,长度为length的字符串。 18.tolowerstring tolower(string source) 19.toupperstring toupper(string source) 20.to_charstring to_char(bigint/boolean/double/datetime value) 21.trimstring trim(string str) 用途:将指定字符串去除左右空格 22.ltrimstring ltrim(string str) 用途:将指定字符串去除左空格 23.rtrimstring rtrim(string str) 用途:将指定字符串去除左空格 24.reversestring reverse(string str) 25.spacestring space(bigint n) 用途:空格字符串函数,返回长度为n的空字符串。 26.repeatstring repeat(string str, bigint n) 用途:返回重复n次后的str字符串。 二、窗口函数 1.window_func() over (partition by col1, [col2…] order by col1 [desc|asc], col2[asc|desc] windowing_clause)开窗函数,支持这种开窗函数的有avg/count/max/min/stddev/sum,一般都是用于分区表的操作。 2.countbigint count([distinct] expr) over (partition by col1), [col2…] order by col1[asc|desc] window_clause 计算计数值 select user_id, count(user_id) over (partition by user_id) as count from test_src; select user_id count(user_id) over (partirion by user_id order by user_id) as count from test_src; 3.avgavg([distinct] expr) over (partition by col1)[, col2…] [order by col1 [asc|desc]] [,col2[asc|desc]…]] [window_clause]) 计算平均值 4.max(min)max([distinct] expr) over (partition by col1)[, col2…] [order by col1 [asc|desc]] [,col2[asc|desc]…]] [window_clause]) 计算最大值|最小值 5.mediandouble median([distinct] expr) over (partition by col1)[, col2…] [order by col1 [asc|desc]] [,col2[asc|desc]…]] [window_clause]) decimal median([distinct] expr) over (partition by col1)[, col2…] [order by col1 [asc|desc]] [,col2[asc|desc]…]] [window_clause]) 计算中位数 6.stddev总体标准差 double stddev([distinct] expr) over (partition by col1)[, col2…] [order by col1 [asc|desc]] [,col2[asc|desc]…]] [window_clause]) decimal stddev([distinct] expr) over (partition by col1)[, col2…] [order by col1 [asc|desc]] [,col2[asc|desc]…]] [window_clause]) 7.stddev_samp样本标准差 double stddev([distinct] expr) over (partition by col1)[, col2…] [order by col1 [asc|desc]] [,col2[asc|desc]…]] [window_clause]) decimal stddev([distinct] expr) over (partition by col1)[, col2…] [order by col1 [asc|desc]] [,col2[asc|desc]…]] [window_clause]) 8.sum计算汇总值 sum([distinct] expr) over (partition by col1)[, col2…] [order by col1 [asc|desc]] [,col2[asc|desc]…]] [window_clause]) 9.dense_rankbigint dense_rank() over(partition by col1[, col2…]) order by col1[asc|desc] [col2[asc|desc]] 计算连续排名。 10.rankbigint rank() over (partition by col1[, col2…] order by col1 [asc|desc][, col2[asc|desc]…]) 计算排名。col2相同的行数获得排名顺序下降 11.laglag(expr, bigint offset, default) over (partition by col1[, col2…]) order by col1[asc|desc][, col2[asc|desc]…]) 按照偏移量取当前行之前第几行的值,如果当前行号为rn,则取行号为rn-offset的值。 12。leadlead(expr, bigint offset, default) over (partition by col1[, col2…]) order by col1[asc|desc][, col2[asc|desc]…]) 按照偏移量取当前行之后第几行的值,如果当前行号为rn,则取行号为rn+offset的值。 13.percent_rankpercent_rank() over partition by col1[, col2…] order by col1 [asc|desc][, col2[asc|desc]…] 计算一组数据中某行的相对排名 14.row_numberrow_number() over partition by col1[, col2…] order by col1 [asc|desc][, col2[asc|desc]…] 计算行号,从1开始 15.cluster_sampleboolean cluster_sample(bigint x[, bigint y]) over(partition by col1[, col2…]) 分组抽样 例如:有两个实验分组,group_a和group_b,每个实验分组的数值value都是几千个,现在要从a组和b组中随机抽样10%,用sql语句如何实现? select group_id, value from( select group_id, value, cluster_sample(10,1) over (partition by group_id) as flag from tbl ) sub where flag = true; 三、聚合函数 1.countbigint count([distinct|all value) 计算记录数,可以与聚合函数搭配使用,类似map_reduce里面的combiner 2.avgdouble avg(double value) decimal avg(double value) 计算平均数;如果value是bigint,会隐式转换为double类型后参与运算 3.maxmax(value) 计算最大值,value是什么类型,返回值就是什么类型 4.minmin(value) 计算最小值,value是什么类型,返回值就是什么类型 5.mediandouble median(double number) decimal median(decimal mumber) 计算中位数 6.stddevdouble stddev(double number) decimal stddev(decimal number) 计算总体标准差 7.stddev_sampdouble stddev_samp(double number) decimal stddev_samp(decimal number) 计算样本标准差 8.sumsum(value) 计算汇总值,value是什么类型,返回什么类型 9.wm_concatstring wm_concat(string separator, string str) 用指定的分隔符,把str衔接起来 10.castcast(expr as ) 将表达式的结果转换为指定的类型,例如: cast(‘120’ as string) cast(double as bigint) cast(string as bigint) cast(string as datetime) 或者 cast(datetime as string) 会采用默认的日期格式yyyy-mm-dd hh:mm:ss 11.coalescecoalesce(expr1, expr2, …) 返回参数列表中第一个非NULL的值,如果列表中全部都是NULL则返回NULL 12.decodedecode(expression, search, result[, search, result]…[,default]) 实现if-then-else分支选择的功能 以下两个案例是等值的 select decode(customer_id 1, ‘taobao’, 2, ‘alipay’, 3, ‘aliyun’, NULL, ‘N/A’ ‘others’) as result from sales_detail; select (if customer_id := 1 then result := ‘taobao’; elseif customer_id := 2 then result := ‘alipay’; elseif customer_id := 3 then result := ‘aliyun’; elseif customer_id := null then result := ‘N/A’ ) else result := ‘others’ end if; ) from sales_detail; 13.greatestgreatest(var1, var2, …) 返回输入参数中最大的一个 14.ordinalordinal(bigint nth, var1, var2, …) 将输入变量从小到大排序后,返回nth指定的位置的值。 15.leastleast(var1, var2, …) 返回输入参数中最小的一个 16.uuiduuid() 返回一个随机ID 17.sampleboolean sample(x, y, column_name) 对所有读入的column_name的值,哈希x份,取第y份。其中y和colum_name同时存在或者同时省略 18.case whencase value when (condition1) then result1 when (condition2) then result2 … else resultn end case when (condition1) then result1 when (condition2) then result2 … else resultn end 19.ifif(testcondition, valuetrue, valuefalseornull) 判断testcondition是否为真,真取第一个数值,否则取第二个数值。 四、日期函数 1.dateadddatetime dateadd(datetime date, bigint delta, string datepart); 用途:按照指定的单位datepart和幅度delta修改date的值 2.datediffbigint datediff(datetime date1, datetime date2, string datepart); 用途:计算两个时间date1, date2 在指定时间单位datepart的差值 3.datepartbigint datepart(datetime date, string datepart) 用途:提取日期date中指定的时间单位datepart的值 4.datetruncdatetime datetrunc(datetime date, string datepart) 用途:返回日期date被截取指定时间单位datepart后的日期值 5.from_unixtimedatetime from_unixtime(bigint unixtime) 用途:将数字型的unix时间日期值unixtime 转为日期值 6.UNIX_TIMESTAMPbigint unix_timestamp(datetime date) 用途:将日期值转换成unixtime数值 7.GETTIMEdatetime gettime() 用途:获取计算的当前日期值 8.ISDATEboolean isdate(string date, string format) 用途:判断一个日期字符能否基于对应的格式转换为一个日期值,如果可以返回true,否则返回false 9.lastdaydatetime lastday(datetime date) 用途:取date当月的最后一天,截取到天,时分秒部分为00:00:00 10.to_datedatetime to_date(string date, string format) 用途:将一个字符串date按照format的指定的格式转换为日期值 11.to_charstring to_char(datetime date, string fromat) 用途:将一个日期值按照指定的格式转换为字符串 12.weekdaybigint weekday(datetime date) 用途:返回date 日期当前周的第几天 13.weekofyearbigint weekofyear(datetime date) 用途:返回date在那一年中的第几周 五、数学函数 1.absdouble abs(double number) bigint abs(bigint number) decimal abs(decimal number) 返回绝对值 select abs(id) from tb1; 2.acosdouble acos(double number) decimal acos(decimal number) 计算number的反余玄函数 3.asindouble asin(double number) decimal asin(decimal number) 计算number的反正玄函数 4.atandouble atan(double number) 反正切函数 5.ceilbigint ceil(double number) bigint ceil(decimal number) 返回不小于number的最小整数 6.convstring conv(string input, bigint from_base, bigint to_base) 进制转换函数 例如: conv(‘1111111001010101’, 2, 10) 7.cosdouble cos(double number) decimal cos(decimal number) 计算number的余玄函数 8.coshdouble cosh(double number) decimal cosh(decimal number) 计算number的双余玄函数 9.cotdouble cot(double number) decimal cot(decimal number) 计算number的余切函数 10.expdouble exp(double number) decimal exp(decimal number) 计算number的双余玄函数 11.floorbigint floor(double number) bigint floor(decimal number) 向下取整,返回比number小的整数值 floor(1.2) = 1 floor(-1.2) = -2 floor(0.1) = 0 12.lndouble ln(double number) decimal ln(decimal number) 计算number的自然对数 13.logdouble log(double base, double x) decimal log(decimal base, decimal x) 返回以base为底的x的对数 14.powdouble pow(double x, double, y) decimal pow(decimal x, decimal, y) 返回x的y次方 15.randdouble rand(bigint seed) 返回以seed为种子的double随机数,返回值区间是的0-1; 16.rounddouble round(double number, [bigint decimal_places]) decimal round(decimal number, [bigint decimal_places]) 四舍五入到指定小数点 decimal_places可以是负数,负数会从小数点向左开始计数,并且不保留小数部分; round(123.345, -2) = 100.0 round(123.345, -4) = 0.0 17.signsign(x) x为正值,返回1.0 x为负值,返回-1.0 当x为0, 返回0.0 当x为空,抛出异常 18.sindouble sin(double number) 正玄函数 19.sinhdouble sinh(double number) decimal sinh(decimal number) 双曲正玄函数 20.sqrtdouble sqrt(double number) decimal sqrt(decimal number) 计算平方根 21.tandouble tan(double number) decimal tan(decimal number) 计算正切函数 21.tanhdouble tanh(double number) decimal tanh(decimal number) 计算双曲正切函数 22.truncdouble trunc(double number[, bigint decimal_places]) decimal trunc(decimal number[, bigint decimal_places]) 将输入值number截取到指定小数点的位置,截取掉的部分补0,decimal_places也可以是负数,如果是负数,从小数点向左开始截取,并且不保留小数部分;如果decimal_places超过了整数部分长度,返回0 trunc(125.815) = 125.0 trunc(125.815,0)=125.0 trunc(125.815,1)=125.8 trunc(125.815,-1)=120.0 |
CopyRight 2018-2019 实验室设备网 版权所有 |